package com.lf.api.weixin.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lf.api.Constants;
import com.lf.api.weixin.WeiXinUtil;

/**
 * 
 * Description: 定时获取微信JSTicket
 * Copyright: Copyright (c)2012
 * Company: 小小书童
 * @author: 小小书童
 * @version: 1.0
 * Create at: 2016年10月1日 下午11:34:14
 *
 * Modification History:
 * Date Author Version Description
 * ---------------------------修改历史---------------------------------------
 * 2016年10月1日 小小书童 1.0
 */
public class FetchJSTicketJob implements Job {
	private static Logger log = LoggerFactory.getLogger(FetchJSTicketJob.class);

	@Override
	public void execute() {
		try {
			log.info("开始获取js_ticket。。。");
			JSTicket ticket = WeiXinUtil.getJSTicket(Constants.WeiXinCache.ACCESS_TOKEN);
			if (null != ticket) {
				// 保存到系统全局变量里
				Constants.WeiXinCache.JSAPI_TICKET = ticket.getTicket();
				log.info("获取js_ticket成功，有效时长{}秒 ticket:{}", ticket.getExpiresIn(), ticket.getTicket());
				// 休眠7000秒
				Thread.sleep((ticket.getExpiresIn() - 200) * 1000);
			} else {
				// 如果access_token为null，3秒后再获取
				Thread.sleep(3 * 1000);
			}
		} catch (InterruptedException e) {
			try {
				Thread.sleep(60 * 1000);
			} catch (InterruptedException e1) {
				log.error("{}", e1);
			}
			log.error("{}", e);
		}

	}

}
